package com.live.webvideo.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.live.webvideo.common.TencentVideo.TencentCloudUtils;
import com.live.webvideo.common.base.BaseController;
import com.live.webvideo.common.util.ApiResult;
import com.live.webvideo.common.util.Constant;
import com.live.webvideo.common.util.RdPage;
import com.live.webvideo.common.util.ToolUtil;
import com.live.webvideo.project.entity.RoomInfo;
import com.live.webvideo.project.service.RoomInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangdong
 * @date 2018/10/27 20:52
 * 描述：视频首页
 */
@Api(description = "视频首页接口", value = "用户首页接口")
@Scope("prototype")
//@RestController
@Controller
@EnableAutoConfiguration
public class IndexController extends BaseController {

    @Autowired
    private RoomInfoService roomInfoService;

    /**
     * 视频首页
     *
     * @return
     */
    @ApiOperation(value = "视频首页")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        PageHelper.startPage(Integer.valueOf(1), Integer.valueOf(10));

        List<RoomInfo> list = roomInfoService.selectRoomLists();

        PageInfo<RoomInfo> pageInfo = new PageInfo<>(list);

        RdPage rdPage = ToolUtil.getPage(pageInfo, Integer.valueOf(1), Integer.valueOf(10));
        model.addAttribute("roomList",list);

        return "index";
    }

    /**
     * 节目
     *
     * @return
     */
    @ApiOperation(value = "节目")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/shows", method = RequestMethod.GET)
    public String shows() {

        return "shows";
    }

    /**
     * 视频详情页
     *
     * @return
     */
    @ApiOperation(value = "视频详情页")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/single", method = RequestMethod.GET)
    public String single(Model model, @PathVariable("id") @Valid String id) {
        model.addAttribute("id",id);
        return "single";
    }

    /**
     * 视频详情页
     *
     * @return
     */
    @ApiOperation(value = "视频详情页")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/single/{id}", method = RequestMethod.GET)
    public String singleid(Model model, @PathVariable("id") String id) {
        model.addAttribute("id",id);
        return "single";
    }

    /**
     * 视频详情页
     *
     * @return
     */
    @ApiOperation(value = "视频详情页")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/singleDate", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<Map<String,Object>> singleDate() {
        ApiResult<Map<String,Object>> result = null;
        Map<String,Object> map = new HashMap<>();
        //房间号
        String roomId = ToolUtil.getCodeEight();
        //推流地址生成
        String pushUrl = TencentCloudUtils.getPushUrl(roomId);
        //PC拉流地址获得
        String pullRtmpUrl = TencentCloudUtils.getPullRtmpUrl(roomId);
        //获取关闭直播的url关闭直播
        String closeLiveUrl = TencentCloudUtils.getCloseLiveUrl(roomId);
        String appPull = TencentCloudUtils.getPullUrl(roomId);


        map.put("pushUrl",pushUrl);
        map.put("pullRtmpUrl",pullRtmpUrl);
        map.put("closeLiveUrl",closeLiveUrl);
        map.put("appPull",appPull);

        System.out.println("pushUrl:"+pushUrl);
        System.out.println("pullRtmpUrl:"+pullRtmpUrl);
        System.out.println("closeLiveUrl:"+closeLiveUrl);
        System.out.println("appPull:"+appPull);

        result = new ApiResult<>(Constant.SUCCEED_CODE_VALUE, Constant.OPERATION_SUCCESS, map, null);

        return result;
    }

    /**
     * 历史
     *
     * @return
     */
    @ApiOperation(value = "历史")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/history", method = RequestMethod.GET)
    public String history() {

        return "history";
    }

    /**
     * 宽布局
     *
     * @return
     */
    @ApiOperation(value = "宽布局")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/pageWide", method = RequestMethod.GET)
    public String pageWide() {

        return "page-wide";
    }

    /**
     * 固定页脚页面
     *
     * @return
     */
    @ApiOperation(value = "固定页脚页面")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/pageFixedFooter", method = RequestMethod.GET)
    public String pageFixedFooter() {

        return "page-fixed-footer";
    }

    /**
     * 固定页脚家庭
     *
     * @return
     */
    @ApiOperation(value = "固定页脚家庭")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/indexFixedFooter", method = RequestMethod.GET)
    public String indexFixedFooter() {

        return "index-fixed-footer";
    }

    /**
     * 博客
     *
     * @return
     */
    @ApiOperation(value = "博客")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/blog", method = RequestMethod.GET)
    public String blog() {

        return "blog";
    }

    /**
     * 联系
     *
     * @return
     */
    @ApiOperation(value = "联系")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contact() {

        return "contact";
    }

}
